package com.lagou.controller;

import com.lagou.pojo.Resume;
import com.lagou.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 执行登录
     *
     * @return
     */
    @RequestMapping("/login/doLogin")
    public String dologin(String username, String password, HttpSession session) {
        if ("admin".equals(username) && "admin".equals(password)) {
            session.setAttribute("username", username);
            return "redirect:/resumeList";
        } else {
            return "redirect:/login";
        }
    }

    /**
     * 打开列表页面
     *
     * @return
     */
    @RequestMapping("/resumeList")
    public String resumeList() {
        return "resumeList";
    }

    /**
     * 加载列表数据
     *
     * @return
     */
    @RequestMapping("/resumeListData")
    @ResponseBody
    public List<Resume> getResumeList() {
        List<Resume> resumeList = resumeService.getResumeList();
        return resumeList;
    }

    /**
     * 打开新增/修改页面
     *
     * @return
     */
    @RequestMapping("/saveEdit")
    public String saveEdit(Long id, Model model) {
        if (id != null) {
            Resume resume = resumeService.getResume(id);
            model.addAttribute("resume", resume);
        }
        return "resumeEdit";
    }

    /**
     * 执行新增/修改
     *
     * @return
     */
    @RequestMapping("/save")
    public String save(Resume resume) {
        resumeService.saveResume(resume);
        return "redirect:resumeList";
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Long id) {
        resumeService.deleteResume(id);
        return "redirect:resumeList";
    }
}
